
package com.fgba.express.comm.result;

import lombok.Data;

import java.io.Serializable;

/**
 * @param <T>
 * @author fgba
 */
@Data
public class Result<T> implements Serializable {

    private String code;

    private T content;

    private String msg;

    public static <T> Result<T> success() {
        return success(null);
    }

    public static <T> Result<T> success(T content) {
        ResultCode rce = ResultCode.SUCCESS;
        if (content instanceof Boolean && Boolean.FALSE.equals(content)) {
            rce = ResultCode.SYSTEM_EXECUTION_ERROR;
        }
        return result(rce, content);
    }


    public static <T> Result<T> error() {
        return result(ResultCode.SYSTEM_EXECUTION_ERROR.getCode(), ResultCode.SYSTEM_EXECUTION_ERROR.getMsg(), null);
    }

    public static <T> Result<T> error(String msg) {
        return result(ResultCode.SYSTEM_EXECUTION_ERROR.getCode(), msg, null);
    }

    public static <T> Result<T> error(String code, String msg, T content) {
        return result(code, msg, content);
    }

    public static <T> Result<T> status(boolean status) {
        if (status) {
            return success();
        } else {
            return error();
        }
    }

    public static <T> Result<T> custom(IResultCode resultCode) {
        return result(resultCode.getCode(), resultCode.getMsg(), null);
    }


    private static <T> Result<T> result(IResultCode resultCode, T content) {
        return result(resultCode.getCode(), resultCode.getMsg(), content);
    }

    private static <T> Result<T> result(String code, String msg, T content) {
        Result<T> result = new Result<T>();
        result.setCode(code);
        result.setContent(content);
        result.setMsg(msg);
        return result;
    }

}
